/**
 * @file   AbstractPacket.h
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-21
 * @brief  Base object for a DON packet.
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DON_CORE_PACKET_ABSTRACTPACKET_H
#define DON_CORE_PACKET_ABSTRACTPACKET_H

#include <DON/Core/Packet/PacketType.h>
#include <QtCore/QList>

namespace DON
{
namespace Core
{
namespace Packet
{

/**
 * @brief The AbstractPacket class
 */
class LIBDONQT4CORESHARED_EXPORT AbstractPacket
{
public:
    /**
     * @brief Constructor
     */
    AbstractPacket();

    /**
     * @brief Constructor
     * @param source Packet's Source address
     * @param destination Packet's Destination address
     * @param packetType Packet type
     * @param sequence Sequence number
     */
    AbstractPacket(const quint16 source,
                   const quint16 destination,
                   const PacketType packetType,
                   const quint16 sequence);

    /**
     * @brief Copy constructor
     * @param other Object to be copied
     */
    AbstractPacket(const AbstractPacket &other);

    /**
     * @brief Destructor
     */
    virtual ~AbstractPacket();

    /**
     * @brief Assignment operator
     * @param other Object to be copied
     * @return This object
     */
    AbstractPacket & operator=(const AbstractPacket &other);

    /**
     * @brief Checks if packet is valid
     * @retval true Packet is valid
     * @retval false Packet is not valid
     */
    virtual bool isValid() const = 0;

    /**
     * @brief Checks if this is a GenericPacket instance
     * @retval true This is a GenericPacket instance
     * @retval false This is not a GenericPacket instance
     *
     * @note Default implementation returns false
     */
    virtual bool isGenericPacketInstance() const;

    /**
     * @brief Get packet's Source address
     * @return Packet's Source address
     */
    quint16 getSource() const;

    /**
     * @brief Set packet's Source address
     * @param source Packet's Source address
     */
    void setSource(const quint16 source);

    /**
     * @brief Get packet's Destination address
     * @return Packet's Destination address
     */
    quint16 getDestination() const;

    /**
     * @brief Set packet's Destination address
     * @param destination Packet's Destination address
     */
    void setDestination(const quint16 destination);

    /**
     * @brief Get packet type
     * @return Packet type
     */
    PacketType getPacketType() const;

    /**
     * @brief Set packet type
     * @param packetType Packet type
     * @retval true Success
     * @retval false Error
     */
    bool setPacketType(const PacketType packetType);

    /**
     * @brief Get sequence number
     * @return Sequence number
     */
    quint16 getSequence() const;

    /**
     * @brief Set sequence number
     * @param sequence Sequence number
     */
    void setSequence(const quint16 sequence);

    /**
     * @brief Get maximum number of items in payload data
     * @return Maximum number of items in payload data
     */
    static int getMaxPayloadDataItemCount();

    /**
     * @brief Create binary representation of the packet
     * @return Binary data
     */
    virtual QByteArray toBinary() const;

protected:
    /**
     * @brief Create binary representation of the packet
     * @param payloadData Payload data
     * @return Binary data
     */
    QByteArray toBinary(const QByteArray &payloadData) const;

    /**
     * @brief Get the packet's payload data items
     * @param[out] ok Error status output (true --> success, false --> error)
     * @return List of payload data items
     */
    virtual QList<QByteArray> getPayloadDataItems(bool *ok = NULL) const = 0;

private:
    class Private;
    Private *d;
};

}
}
}

#endif // DON_CORE_PACKET_ABSTRACTPACKET_H
